<?php
// function __autoload($class_name) {
//     include 'class.' . strtolower($class_name) . '.php';
// }

require_once 'class.database.php';

class DokumenDao {
    
    public function saveDokumen($judul,$nama_file,$id_usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="INSERT INTO t_dokumen(judul,nama_file,id_usulan) ";
        $query.=" VALUES('$judul','$nama_file','$id_usulan')";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function saveDokumenPeg($judul,$nama_file,$id_usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="INSERT INTO t_dokumen(judul,nama_file,id_usulan_peg) ";
        $query.=" VALUES('$judul','$nama_file','$id_usulan')";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function saveDokumenDetail($judul,$nama_file,$id_det_usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="INSERT INTO t_dokumen(judul,nama_file,id_det_usulan) ";
        $query.=" VALUES('$judul','$nama_file','$id_det_usulan')";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }
    public function getListDokumen($id_usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_dokumen WHERE id_usulan='$id_usulan'";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function getListDokumenUsulanPeg($id_usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_dokumen WHERE id_usulan_peg='$id_usulan'";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function getListDokumenPeg($id_det_usulan){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_dokumen WHERE id_det_usulan='$id_det_usulan'";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }
    
    public function getDokumen($id_dokumen){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_dokumen WHERE id_dokumen='$id_dokumen'";
    
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
    }
    
    public function hapusFile($id_dokumen){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_dokumen WHERE id_dokumen='$id_dokumen'";
        
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }
}

class Dokumen{
    var $id_dokumen;
    var $id_usulan;
    var $judul;
    var $nama_file;
}
?>
